package com.party.admin.web.controller.system;

import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.system.Dict;
import com.party.core.service.system.IDictService;
import com.party.core.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 系统字典控制器
 * Created by yifeng
 *
 * @date 2018/3/19 0019
 * @time 下午 16:04
 */
@Controller
@RequestMapping("/system/dict")
public class DictController {

    @Autowired
    private IDictService dictService;

    /**
     * 字典类型列表
     *
     * @param dict
     * @param page
     * @return
     */
    @RequestMapping("typeList")
    public ModelAndView typeList(Dict dict, Page page) {
        page.setLimit(20);
        dict.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        List<Dict> types = dictService.typeList(dict, page);
        ModelAndView mv = new ModelAndView("system/dict/typeList");
        mv.addObject("types", types);
        mv.addObject("dict", dict);
        mv.addObject("page", page);
        return mv;
    }

    /**
     * 字典数据列表
     *
     * @param dict
     * @return
     */
    @RequestMapping("dictList")
    public ModelAndView dictList(Dict dict) {
        dict.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        List<Dict> dicts = dictService.webListPage(dict, null);
        ModelAndView mv = new ModelAndView("system/dict/dictList");
        mv.addObject("dicts", dicts);
        mv.addObject("dict", dict);
        return mv;
    }

    /**
     * 新增/编辑字典页面
     *
     * @param dict
     * @return
     */
    @RequestMapping("view")
    public ModelAndView addView(Dict dict) {
        ModelAndView mv = new ModelAndView("system/dict/view");
        if (StringUtils.isNotEmpty(dict.getType())) {
            List<Dict> types = dictService.typeList(dict, null);
            if (types.size() > 0) {
                Dict myDict = types.get(0);
                mv.addObject("myDict", myDict);
            }
        }
        if (StringUtils.isNotEmpty(dict.getId())) {
            Dict dictDb = dictService.get(dict.getId());
            mv.addObject("dict", dictDb);
        }
        return mv;
    }

    /**
     * 保存字典
     *
     * @param dict
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    public AjaxResult save(Dict dict) {
        try {
            if (StringUtils.isEmpty(dict.getId())) {
                dictService.insert(dict);
            } else {
                Dict dbDict = dictService.get(dict.getId());
                MyBeanUtils.copyBeanNotNull2Bean(dict, dbDict);
                dictService.update(dbDict);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("保存失败");
        }
    }

    @ResponseBody
    @RequestMapping("updateStatus")
    public AjaxResult updateStatus(String id, Integer openStatus) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        if (openStatus == null) {
            return AjaxResult.error("状态不能为空");
        }
        try {
            Dict dict = dictService.get(id);
            dict.setOpenStatus(openStatus);
            dictService.update(dict);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("更新失败");
        }
    }

    @ResponseBody
    @RequestMapping("delete")
    public AjaxResult delete(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }

        try {
            dictService.deleteLogic(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }
}
